{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0bfc2ec3",
   "metadata": {},
   "source": [
    "# 委托\n",
    "1. 委托是函数(方法)的容器,可以理解为表示函数的变量类型,用来存储传递函数方法。委托本质是一个类,用来定义函数的类型(返回值和参数的类型)。\n",
    "\n",
    "2. 基础语法: 访问修饰符 delegate 返回值 委托名(参数列表),默认不写为 public;可以声明在namespace和class语句块中\n",
    "\n",
    "3. 使用: 作为类的成员,作为函数的参数\n",
    "\n",
    "4. 委托变量可以存多个函数会依次执行所有添加的函数+即可,删除函数-即可,多减不会报错\n",
    "\n",
    "5. 系统提供的委托"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "06bb6092",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n",
      "Hello, World!\n",
      "Hello, World!\n",
      "Hello, World!\n"
     ]
    }
   ],
   "source": [
    "//声明了可以用来存储的无参无返回值函数的容器\n",
    "public delegate void MyDelegate(string message); //同一个语句块中,委托规则的申明是不能重名的\n",
    "\n",
    "void test(string message)\n",
    "{\n",
    "    Console.WriteLine(message);\n",
    "}\n",
    "\n",
    "//使用:装载同类型的函数\n",
    "MyDelegate del = new MyDelegate(test);\n",
    "del(\"Hello, World!\");\n",
    "del.Invoke(\"Hello, World!\");\n",
    "\n",
    "MyDelegate del2 = test; //委托可以直接赋值\n",
    "del2(\"Hello, World!\");\n",
    "del2.Invoke(\"Hello, World!\");\n",
    "\n",
    "//在类的成员中\n",
    "class MyClass {\n",
    "    public MyDelegate del; //存放函数类型\n",
    "\n",
    "    public void TestFuc(MyDelegate myDel)\n",
    "    {\n",
    "        //先处理其他逻辑,当这些逻辑处理完了再执行传入的函数\n",
    "        this.del = myDel; //将传入的函数存储到类的成员中\n",
    "        myDel(\"Hello from MyClass!\");\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World\n",
      "Hello World\n",
      "Hello World\n"
     ]
    }
   ],
   "source": [
    "MyDelegate myDelegate = test;\n",
    "myDelegate += test; //可以将多个函数添加到委托中,myDelegate会依次执行所有添加的函数\n",
    "myDelegate(\"Hello World\");\n",
    "myDelegate -= test; //可以删除委托中的函数\n",
    "myDelegate(\"Hello World\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World\n",
      "42\n",
      "10\n",
      "Name: Alice, Age: 30\n",
      "Sum: 15\n"
     ]
    }
   ],
   "source": [
    "// 系统自带的委托\n",
    "using System;\n",
    "// Action 委托是无参无返回值的函数\n",
    "Action myAction = () => Console.WriteLine(\"Hello World\");\n",
    "myAction();\n",
    "// Func 委托是有返回值的函数,泛型委托\n",
    "Func<int> myFunc = () => 42;\n",
    "Console.WriteLine(myFunc()); // 输出 42\n",
    "// 泛型委托\n",
    "delegate T MyGenericDelegate<T>(T value); //可以存储任意类型的函数\n",
    "MyGenericDelegate<int> intDelegate = (x) => x * 2;\n",
    "Console.WriteLine(intDelegate(5)); // 输出 10\n",
    "// 可以传N个参数的委托\n",
    "Action<string, int> myActionWithParams = (name, age) => \n",
    "{\n",
    "    Console.WriteLine($\"Name: {name}, Age: {age}\");\n",
    "};\n",
    "myActionWithParams(\"Alice\", 30); // 输出 Name: Alice, Age: 30\n",
    "\n",
    "Func<int, int, string> myFuncWithParams = (x, y) => \n",
    "{\n",
    "    return $\"Sum: {x + y}\";\n",
    "};\n",
    "Console.WriteLine(myFuncWithParams(5, 10)); // 输出 Sum: 15"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "polyglot-notebook"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
